Create .htaccess File

28-12-17 Course- SEO

The .htaccess file is a configuration file for use on the web server running the Apache web server software. Using the .htaccess file, we can do the following things;

  • Block bots
  • Block or Allow an IP
  • Resolve Canonical Issue
  • Prevent Directory Listing
  • Prevent viewing of .htaccess file
  • Manage Custom Error Pages (400, 404, 500, etc)
  • Redirect File or Directory to specific URL
  • Set default page to display when a directory is accessed

Redirect WWW to non-WWW


RewriteEngine on
rewritecond %{http_host} ^fastread.in [nc]
rewriterule ^(.*)$ http://fastread.in/$1 [r=301,nc]

Redirect non-WWW to WWW


RewriteEngine on
rewritecond %{http_host} ^fastread.in [nc]
rewriterule ^(.*)$ http://www.fastread.in/$1 [r=301,nc]

.htaccess: This file is mainly used for redirect url, to create this file you can create any file and save with .htaccess file name.

Custom Error Pages for Better SEO

One use of the .htaccess file is to redirect users to a custom error page depending on the specific web server error they encounter.

.htaccess file for Custom Error Pages


ErrorDocument 404 /filenotfound.html

.htaccess file for Custom Error Pages


//Custom 400 errors
ErrorDocument 400 /error400.html

//Custom 401 errors
ErrorDocument 401 /error401.html

//Custom 403 errors
ErrorDocument 403 /error403.html

//Custom 404 errors
ErrorDocument 404 /error404.html

//Custom 500 errors
ErrorDocument 500 /error500.html

If you wanted to redirect users for another error, such as 500, Internal Server Error, you would do it like this (and so on)

.htaccess file for Custom Error Pages


ErrorDocument 500 /filenotfound.html

DirectoryIndex Uses

Using directoryindex command we can specify a default page to display when a directory is accessed.

DirectoryIndex Uses


DirectoryIndex index.html index.cgi index.php

In above syntax, if there was no file named index.html in your directory, then the server would look for a file called index.cgi. If that file wasn't present, it would look for one called index.php, and so on.

Comment for Htaccess File

Use # for comment in htaccess file

Comment in htaccess File


# BLOCK SITE REFERRERS
RewriteEngine on
 RewriteCond %{HTTP_REFERER} \<\?\=\$block_referrer\?\> [NC,OR]
 RewriteCond %{HTTP_REFERER} http\://www\.aitechtonic\.com/ [NC,OR]
 RewriteCond %{HTTP_REFERER} http\://www\.myworth\.ga/ [NC,OR]

Redirect All Files with Certain Extension

Redirect all .php extension file on .html file

Redirect All .php URL on .html Extension File


RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.htm [R=301,L]

Redirect all .html Extension File on .php Extension File

Redirect all .html Extension File on .php Extension File


RewriteEngine On
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).htm$ /$1.php [R=301,L]

Redirect HTTPS non-www to HTTPS www using .htaccess

Redirect HTTPS non-www to HTTPS www using .htaccess


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]